{% extends "layout.html" %}

{% block head %}
  {% endblock %}

{% block right %}
<style>
    .layui-table td, .layui-table th {
    min-width: 74px;
}
</style>

<div class="layui-fluid" id="mender" title={{session["user_id"]}}>
    <div class="layui-row layui-col-space15"  id="menderPower" title={{session["power"]}}}>
        <div class="layui-col-md12">
            <div class="layui-card">

<!--                搜索-->
                <div class="layui-card-body ">
                    <form class="layui-form layui-col-space5" action="/select_user" method="post">
                        <div class="layui-inline layui-show-xs-block">
                            <input type="text"  name="user_id"  placeholder="请输入账号" autocomplete="off" class="layui-input">
                        </div>
                        <div class="layui-inline layui-show-xs-block">
                            <button class="layui-btn" type="submit" lay-submit="" lay-filter="sreach"><i class="layui-icon">&#xe615;</i></button>
                            {% if  session["selectReturn"] ==0 %}
                            <a  href="/member-list" class="layui-btn"  lay-submit="" lay-filter="sreach"><i class="layui-icon">
                            返回</i></a>
                            {% endif  %}
                        </div>
                    </form>
                </div>





                <div class="layui-card-body layui-table-body layui-table-main">
                    <table class="layui-table layui-form">


<!--                      表头  -->
                        <thead>
                          <tr>
                            <th>ID</th>
                            <th>用户名</th>
                            <th>邮箱</th>
                            <th>学历</th>
                            <th>工作经验</th>
                            <th>当前工作</th>
                            <th>当前薪资</th>
                            <th>预测工作01</th>
                            <th>预测工作02</th>
                            <th>预测工作03</th>
                            <th>状态</th>
                            <th>权限</th>
                          </tr>
                        </thead>
<!--             用户列表-->
                        <tbody>
                          {% for i in userTuple %}
                          <tr>
                            <td>{{ i[0]}}</td>
                            <td>{{ i[1]}}</td>
                            <td hidden>{{ i[2]}}</td>
                            <td>{{ i[3]}}</td>
                            <td>{{ i[4]}}</td>
                            <td>{{ i[5]}}</td>
                            <td>{{ i[6]}}</td>
                            <td>{{ i[7]}}</td>
                            <td>{{ i[8]}}</td>
                            <td>{{ i[9]}}</td>
                            <td>{{ i[10]}}</td>

<!--                    冻结或开启用户  -->
                            <td class="td-status">
                              {% if i[2]>=0 %}
                                <a onclick="member_stop(this)" id ={{i[0] }} name={{i[2]}} href="javascript:;" title="停用">
                                    <span class="layui-btn layui-btn-normal layui-btn-mini">已启用</span></a>
                              {% else  %}
                          <a onclick="member_stop(this)" id={{ i[0] }}  name={{ i[2] }} href="javascript:;" title="启用">
                              <span class="layui-btn layui-btn-disabled layui-btn-mini">已停用</span></a>

                              {% endif %}

                            </td>


                            <td >
                                <select   lay-filter="business2"  title= {{ i[0] }} class="select"
                                         lay-verify="type" name="business2"  id={{ i[2] }}  >
                                      {% if i[2]>=2 %}
                                    <option  value="2" selected disabled >超级管理员</option>
                                    <option  value="1" >管理员</option>
                                    <option value="0"  >普通用户</option>
                                      {% elif i[2] == 1  %}
                                   <option value="2"  disabled>超级管理员</option>
                                    <option value="1" selected>管理员</option>
                                    <option value="0"  >普通用户</option>
                                    {% else %}
                                        <option value="2"  disabled>超级管理员</option>
                                    <option value="1" >管理员</option>
                                    <option value="0"  selected>普通用户</option>
                                      {% endif %}
                                </select>
                            </td>
                          </tr>
                      {% endfor  %}


                        </tbody>
                    </table>
                       <br>
                    <br>

                    <div style="text-align: center">

            <form action="/member-list-root" method="post" style="display: inline-block">
                    <button type="submit" class="layui-btn layui-btn-primary layui-btn-sm">第一页</button>
            </form>

                    {% if paging02 !=null  and paging02!=0 %}
                <form action="/member-list-root" method="post" style="display: inline-block">
                        <input name="paging_1" hidden  value={{paging02}}>
                        <button type="submit" class="layui-btn layui-btn-primary layui-btn-sm">上一页</button>
                </form>
                {% endif  %}

                {% if pagingX !=-404 %}
            <form action="/member-list-root" method="post" style="display: inline-block">
                    <input name="paging_1" hidden  value={{paging02+2}}>
                    <button type="submit" class="layui-btn layui-btn-primary layui-btn-sm">下一页</button>
            </form>
                {% endif  %}

                                    <form action="/member-list-root" method="post" style="display: inline-block">
<!--                 最后一页-->
                                    <input name="paging_1" hidden  value=-1>
                    <button type="submit" class="layui-btn layui-btn-primary layui-btn-sm">最后一页</button>
            </form>


<!--                 第几页-->
        <form  action="/member-list-root" method="post" style="display: inline-block">
           跳转到第
           <select  lay-verify="type" class="layui-btn layui-btn-primary layui-btn-sm" name="paging_1" style="width: 50px;" >
                   {% for k  in  range(0,pagingAll) %}
                    {% if k+1!=paging02+1 %}
                  <option   value={{k+1}} selected>{{k+1}}</option>
                    {% endif %}
                   {% endfor %}
               <option   selected  value={{paging02+1}}>{{paging02+1}}</option>
           </select>页
           <button type="submit" class="layui-btn layui-btn-primary layui-btn-sm">确定</button>
                 </form>




                    </div>

                    <br>
                    <br>
                    <br>
                </div>






            </div>
        </div>
    </div>


</div>
<!--    </body>-->
    <script>




      layui.use(['laydate','form'], function(){
        var laydate = layui.laydate;
        var  form = layui.form;
          // 监听全选

                /*用户-修改权限*/
          form.on('select(business2)', function(obj){
           var user_id = obj.elem.title;
           var userPower01 = obj.elem.id;
           var menderPower =  parseInt($('#menderPower').attr('title'));
           var mender =  $('#mender').attr('title');
           var userPower =  parseInt(obj.elem.value);
             layer.confirm('确认要修改吗？', function (index) {
           if(menderPower >= 2 && mender != user_id && userPower01<=1) {

               $.ajax({
                   //请求方式
                   type: "POST",
                   //请求的媒体类型
                   contentType: "application/json;charset=UTF-8",
                   //请求地址
                   url: "/member-edit",
                   //数据，json字符串
                   data: JSON.stringify({
                       'user_id': user_id,
                       'mender': mender,
                       'menderPower': menderPower,
                       'userPower': userPower
                   }),
                   //请求成功
                   success: function (result) {
                       if (result == "success") {
                           layer.msg('修改成功!', {icon: 6, time: 1000});
                       } else if (result == "fail") {
                           layer.open({
                               title: '警告',
                               content: '错误！！！'
                           });
                       }
                   },
                   //请求失败，包含具体的错误信息
                   error: function (e) {
                       console.log(e.status);
                       console.log(e.responseText);
                   }
               });

           }else if( mender == user_id){
             layer.open({
                       title: '警告',
                       content: '禁止修改自己账号！'
                   });
           }else{
                 layer.open({
                       title: '警告',
                       content: '无权限修改！'
                   });
           }
        });
});




        form.on('checkbox(checkall)', function(data){
          if(data.elem.checked){
            $('tbody input').prop('checked',true);
          }else{
            $('tbody input').prop('checked',false);
          }
          form.render('checkbox');
        }); 
        
        //执行一个laydate实例
        laydate.render({
          elem: '#start' //指定元素
        });

        //执行一个laydate实例
        laydate.render({
          elem: '#end' //指定元素
        });
      });


        $('#menderPower').click(function (event) {
            console.log(event.target)
        })





       /*用户-停用*/
      function member_stop(obj){
           var message = $(obj).attr('title');
           var user_id = ($(obj).attr('id'));
           var mender =  ($('#mender').attr('title'));
           var menderPower =  parseInt($('#menderPower').attr('title'));
          var userPower = parseInt($(obj).attr('name'));
           if (menderPower > userPower  ) {
               layer.confirm('确认要' + message + '吗？', function (index) {
                   if ($(obj).attr('title') == '启用') {
                       $.ajax({
                           //请求方式
                           type: "POST",
                           //请求的媒体类型
                           contentType: "application/json;charset=UTF-8",
                           //请求地址
                           url: "/start_user",
                           //数据，json字符串
                           data: JSON.stringify({
                               'user_id': user_id,
                               'mender': mender,
                               'menderPower':menderPower,
                               'userPower' : userPower
                           }),
                           //请求成功
                           success: function (result) {
                               if (result == "success") {
                                     //发异步把用户状态进行更改
                               $(obj).find('i').html('&#xe62f;');
                               $(obj).parents("tr").find(".td-status").find('span').removeClass('layui-btn-disabled').addClass("layui-btn-normal").html('已启用');
                               layer.msg('已启用!', {icon: 6, time: 1000});
                               if(message == "停用"){
                                   $(obj).attr('title', '启用')
                               }else{
                                   $(obj).attr('title', "停用")
                               }
                               } else if (result == "fail") {
                                   layer.open({
                                       title: '警告',
                                       content: '禁止冻结自己账号'
                                   });
                               }
                           },
                           //请求失败，包含具体的错误信息
                           error: function (e) {
                               console.log(e.status);
                               console.log(e.responseText);
                           }
                       });
                   } else {

                       $.ajax({
                           //请求方式
                           type: "POST",
                           //请求的媒体类型
                           contentType: "application/json;charset=UTF-8",
                           //请求地址
                           url: "/stop_user",
                           //数据，json字符串
                           data: JSON.stringify({
                               'user_id': user_id,
                               'mender': mender
                           }),
                           //请求成功
                           success: function (result) {
                               if (result == "success") {
                                       //$(obj).attr('title', '停用');
                                       $(obj).find('i').html('&#xe601;');
                                       $(obj).parents("tr").find(".td-status").find('span').removeClass('layui-btn-normal').addClass('layui-btn-disabled').html('已停用');
                                       layer.msg('已停用!', {icon: 5, time: 1000});
                                        if(message == "停用"){
                                           $(obj).attr('title', '启用')
                                       }else{
                                           $(obj).attr('title', "停用")
                                       }
                                   // $(location).attr('href', '/member-list');
                               } else if (result == "fail") {
                                   layer.open({
                                       title: '警告'
                                       , content: '禁止冻结自己账号'
                                   });

                               }
                           },
                           //请求失败，包含具体的错误信息
                           error: function (e) {
                               console.log(e.status);
                               console.log(e.responseText);
                           }
                       });
                   }
               });
           }else if(mender == user_id) {
                layer.open({
                   title: '警告'
                   , content: '禁止冻结自己账号'
               });
           }
           else if(menderPower <= userPower) {
                layer.open({
                   title: '警告'
                   , content: '无权限冻结账号'
               });
           }

      }





      function delAll (argument) {
        var ids = [];
        // 获取选中的id 
        $('tbody input').each(function(index, el) {
            if($(this).prop('checked')){
               ids.push($(this).val())
            }
        });
  
        layer.confirm('确认要删除吗？'+ids.toString(),function(index){
            //捉到所有被选中的，发异步进行删除
            layer.msg('删除成功', {icon: 1});
            $(".layui-form-checked").not('.header').parents('tr').remove();
        });
      }
    </script>
<!--</html>-->
{% endblock  %}
    {% block footer%}
    {% endblock %}